//package cn.xej.boot_redis01.aop;
//
//import cn.xej.boot_redis01.mapper.TbLogMapper;
//import cn.xej.boot_redis01.pojo.TbLog;
//import cn.xej.boot_redis01.util.SqlUtils;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.*;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//import java.util.Date;
//import java.util.Objects;
//
///**
// * 对用户的操作进行日志管理,排除了登录时的日志
// */
//
//@Aspect
//@Component
//public class AopAspect {
//
//    private final Logger logger = LoggerFactory.getLogger(AopAspect.class);
//
//    @Resource
//    private TbLogMapper tbLogMapper;
//
//    private Date startTime;
//    private TbLog tbLog;
//
//
//    @Autowired
//    private SqlSessionFactory sqlSessionFactory;
//
////    @Pointcut("execution(* cn.xej.security.controller..*.*(..))")
//    @Pointcut("execution(* cn.xej.boot_redis01.controller..*.*(..))")
//    public void basicControllerLog(){}
//
//
//    // 前置通知
//    @Before("basicControllerLog()")
//    public void beforeBasicControllerLog(JoinPoint joinPoint){
//        startTime = new Date();
//        tbLog = new TbLog();
//
//        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
//
//        // uri ---->  /basic/user/list  如访问用户列表
//        String uri = request.getRequestURI();
//
//        // url ---->  http://localhost:7777/basic/user/list   如访问用户列表
//        String url = request.getRequestURL().toString();
//
//        // requestMethod ----> GET  如访问用户列表
//        String requestMethod = request.getMethod();
//
//        // ip ----> 0:0:0:0:0:0:0:1  如访问用户列表
//        String ip = request.getRemoteAddr();
//
//        // 获取方法名 method  ----> list  如访问用户列表
//        String method = joinPoint.getSignature().getName();
//
//        // 获取包加类名 packageMethod ---->  cn.xej.security.controller.basic.TbUserController  如访问用户列表
//        String packageMethod = joinPoint.getSignature().getDeclaringTypeName();
//
//        tbLog.setUrl(url);
////        tbLog.setRequestMethod(requestMethod);
//        tbLog.setIp(ip);
//        tbLog.setMethod(method);
////        tbLog.setPackageMethod(packageMethod);
//    }
//
//
//    @After("basicControllerLog()")
//    public void afterBasicControllerLog(ProceedingJoinPoint pjp) throws IllegalAccessException {
//////        long executionTime = new Date().getTime() - startTime.getTime();
//////        tbLog.setExecutionTime(executionTime);
////        String sql = SqlUtils.getMybatisSql(pjp,sqlSessionFactory);
////        System.out.println(sql);
////
////        tbLogMapper.insert(tbLog);
//    }
//
//    //环绕
//    @Around("basicControllerLog()")
//    public Object around(ProceedingJoinPoint pjp) throws Throwable{
//        System.out.println("++++++++++环绕通知开始+++++++++++");
//
//        //1.从redis中获取主数据库，若获取不到直接退出，否则判断当前数据源是会否为主，若不为主，则切换到主数据源
//        //2.调用目标方法
//        ServletRequestAttributes sra =  (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        if (!Objects.isNull(sra) && !Objects.isNull(sra.getRequest())){
//            HttpServletRequest request = sra.getRequest();
//            System.out.println("url: " + request.getRequestURI());
//            System.out.println("method: "+request.getMethod());      //post or get? or ?
//            System.out.println("class.method: " + pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName());
//            System.out.println("args: "+pjp.getArgs());
//        }
//        Object proceed = pjp.proceed();
//        //3.获取SQL
//        String sql = SqlUtils.getMybatisSql(pjp, sqlSessionFactory);
//        System.out.println("##################################################"+sql+"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
//        System.out.println("++++++++++环绕通知结束+++++++++++");
//
//        return proceed;
//    }
//}
